package cn.hy.redis.key;

public abstract class BasePrefix implements KeyPrefix {
    private final int expireSeconds;
    private final String prefix = "cn.hy.secKill:";
    private final String key;

    private final String COLON = ":";

    public BasePrefix(int expireSeconds, String key) {
        this.expireSeconds = expireSeconds;
        this.key = key;
    }

    public BasePrefix(String prefix) {
        this(-1, prefix);
    }

    @Override
    public int expireSeconds() {
        return expireSeconds;
    }

    /**
     * 可确定获取唯一key
     */
    @Override
    public String getKey(String ... args) {
        StringBuilder sb = new StringBuilder();
        sb.append(prefix).append(key);
        if (args != null  && args.length > 0) {
            for (String arg : args) {
              sb.append(COLON).append(arg);
            }
        }
        return sb.toString();
    }
}